package com.bdqn.controller;

import com.bdqn.entity.UmsReceiverAddress;
import com.bdqn.entity.responseDate.ums.PcdListDto;
import com.bdqn.entity.responseDate.ums.UmsReceiverAddressDto;
import com.bdqn.entity.util.RespBean;
import com.bdqn.service.UmsPcdDicService;
import com.bdqn.service.UmsReceiverAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 用户收货地址表 前端控制器
 * </p>
 *
 * @author hunter
 * @since 2022-04-30
 */
@RestController
@RequestMapping("/ums/receiverAddress")
public class UmsReceiverAddressController {
   @Autowired
   private UmsReceiverAddressService umsReceiverAddressService;
    @GetMapping("/list")
    public RespBean list(Long userId){
        List<UmsReceiverAddressDto> umsReceiverAddressDtos = umsReceiverAddressService.umsReceiverAddressDto(userId);
        if(umsReceiverAddressDtos.size()>0){
            return RespBean.ok("通过用户id查询用户的收货地址成功",umsReceiverAddressDtos);
        }else {
            return RespBean.ok("通过用户id查询用户的收货地址失败",umsReceiverAddressDtos);
        }
    }

    /**
     * 通过 用户id 和收货地址id  进行删除指定的的那个收货地址
     * @param userId
     * @param addressId
     * @return
     */
    @PostMapping("/remove")
    public RespBean remove(Long userId,Long addressId){
        boolean flag = umsReceiverAddressService.removeById(addressId);
        return  flag ? RespBean.ok("删除收货地址成功",flag):RespBean.err("删除收货地址成功");
    }

    @Autowired
    private UmsPcdDicService umsPcdDicService;
    /**
     * 获取省市县列表展示
     */
    @GetMapping("/pcdList")
    public RespBean pcdList() {
        System.out.println("我运行了获取省市县列表的controller方法");
        List<PcdListDto> pcdListDtoList = umsPcdDicService.pcdListDto();
        return pcdListDtoList.size() > 0 ? RespBean.ok("获取省市区列表成功",pcdListDtoList) : RespBean.err("获取省市区列表失败");
    }

    @PostMapping("/saveOrUpdate")
    public RespBean saveOrUpdate(UmsReceiverAddress umsReceiverAddress){
        boolean flag = umsReceiverAddressService.saveOrUpdate(umsReceiverAddress);
        return flag==true?RespBean.ok("修改收货地址成功",flag):RespBean.err("修改收货地址失败");
    }
}
